package com.liuyjy.utils.utils;

/**
 * @Author liuyjy
 * @Date 2023/8/24 14:00
 * @Description: 数字转中文
 **/
public class NumStrConvertor {

    private static final String[] cnNums = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] units = {"千", "百", "十", ""};

    public static void main(String[] args) {
        int[] nums = {1000, 1024, 10, 100024, 920300030, 90000003, 1, 2, 3, 11, -344
        };
        for (int num : nums) {
            System.out.println(cnNumToChineseUnit(num));
        }

        System.out.println(cnNumToChineseUnit("123"));

        System.out.println(cnNumToChinese(123456));
    }


    /**
     * 转换阿拉伯数字到中文字符(带单位)
     *
     * @param num 数字
     * @return String
     */
    public static String cnNumToChineseUnit(int num) {
        if (num == 0) {
            return "零";
        }
        final String illegalPrefix = "一十";
        final String[] units = {"", "万", "亿", "兆"};
        String prefix = "";
        if (num < 0) {
            prefix = "负";
            num = -num;
        }
        String numStr = String.valueOf(num);
        final int metaLen = 4;
        int r = numStr.length() - 1;
        int l = Math.max(0, r - metaLen + 1);
        int unitIndex = 0;
        StringBuilder builder = new StringBuilder();
        while (r >= 0 && l >= 0) {
            builder.insert(0, cnNumToChineseUnit(numStr.substring(l, r + 1)) + units[unitIndex++]);
            r = l - 1;
            l = Math.max(0, r - metaLen + 1);
        }
        String res = builder.toString();
        if (res.startsWith(illegalPrefix)) {
            res = res.substring(1, res.length());
        }
        return prefix + res;
    }

    /**
     * 数字转中文字符串
     *
     * @param num 数字
     * @return String
     */
    public static String cnNumToChinese(int num) {
        if (num == 0) {
            return "零";
        }
        String numStr = String.valueOf(num);
        StringBuilder builder = new StringBuilder();
        int N = numStr.length();
        boolean isPreZero = false;
        for (int i = 0; i < N; i++) {
            final int currNum = numStr.charAt(i) - '0';
            final String currNumCn = cnNums[currNum];
            if (currNum == 0) {
                if (isPreZero) {
                    continue;
                }
                isPreZero = true;
                builder.append(currNumCn);
            } else {
                isPreZero = false;
                builder.append(currNumCn);
            }
        }
        return builder.toString();
    }

    /**
     * 按照中文阅读习惯，处理最高4位的 meta 数字集
     *
     * @param num 数字
     * @return String
     */
    private static String cnNumToChineseUnit(String num) {

        final int metaLen = 4;
        final String zeroStr = "0";
        StringBuilder builder = new StringBuilder();
        int N = num.length();
        boolean canNotJudge = N > metaLen || (N < metaLen && num.startsWith(zeroStr));
        if (canNotJudge) {
            return "";
        }
        boolean isPreZero = false;
        int unitIndex = metaLen - num.length() - 1;
        for (int i = 0; i < N; i++) {
            ++unitIndex;
            final int currNum = num.charAt(i) - '0';
            final String currNumCn = cnNums[currNum];
            final String currUnit = units[unitIndex];
            if (currNum == 0) {
                if (isPreZero) {
                    continue;
                }
                isPreZero = true;
                builder.append(currNumCn);
            } else {
                isPreZero = false;
                builder.append(currNumCn);
                builder.append(currUnit);
            }
        }
        String res = builder.toString();
        return res.endsWith("零") ? res.substring(0, res.length() - 1) : res;
    }


}

